package com.lmu.travel.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lmu.travel.entity.Result;
import com.lmu.travel.entity.User;
import com.lmu.travel.service.UserService;
import com.lmu.travel.util.Create;
import com.lmu.travel.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * (TabUser)表控制层
 *
 * @author liuxuhuang
 * @since 2021-06-27 17:03:54
 */
@Api(tags = "user模块接口")
@RestController
@RequestMapping("user")
public class UserController {
    /**
     * 服务对象
     */
    @Qualifier("UserService")
    @Autowired
    private UserService userService;
    @Autowired
    private RedisUtil redisUtil;


    @PostMapping(value = "register/{VerificationCode}/{email}/{password}")
    @ApiOperation(value = "注册用户")
    @ApiImplicitParams({@ApiImplicitParam(name = "VerificationCode", value = "验证码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "email", value = "邮箱", required = true, dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String")})
    public Result<User> register(@PathVariable("VerificationCode") String VerificationCode, @PathVariable("email") String email, @PathVariable("password") String password) {
        if (redisUtil.get(email).equalsIgnoreCase(VerificationCode)) {
            User user = new User();
            user.setEmail(email);
            user.setPassword(password);
            Create create = new Create();
            user.setUsername(create.getUserName(10));
            if (userService.save(user)) {
                return new Result<User>(200, true, null, "注册成功");
            }
            return new Result<User>(201, false, null, "注册失败");
        } else {
            return new Result<User>(201, false, null, "传入参数错误");
        }
    }

    @PostMapping("login/{username}/{password}")
    @ApiOperation(value = "登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "邮箱/手机号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String")})
    public Result<User> login(@PathVariable("username") String username, @PathVariable("password") String password, HttpServletRequest request) {
        //判断是邮箱还是手机号的正则表达式
        String email = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
        String phone = "^[1][34578]\\d{9}$";
        if (username.matches(phone)) {
            User user = new User();
            user.setTelephone(username);
            user.setPassword(password);
            User user1 = null;
            user1 = userService.loginByPhone(user);
            if (user1 != null) {
                request.getSession().setAttribute("user", user1);
                return new Result<User>(200, true, null, "登录成功");
            }
        } else if (username.matches(email)) {
            User user = new User();
            user.setEmail(username);
            user.setPassword(password);
            User user2 = null;
            user2 = userService.loginByEmail(user);
            if (user2 != null) {
                request.getSession().setAttribute("user", user2);
                return new Result<User>(200, true, null, "登录成功");
            }
        }
        return new Result<User>(201, false, null, "登录失败");
    }
//    @PostMapping("login")
//    @ApiOperation(value = "登录接口")
//    public Result<User> login(User user, HttpServletRequest request) {
//        String username = user.getUsername();
//        String password = user.getPassword();
//        //判断是邮箱还是手机号的正则表达式
//        String email = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
//        String phone = "^[1][34578]\\d{9}$";
//        if (username.matches(phone)) {
//            User user3 = new User();
//            user.setTelephone(username);
//            user.setPassword(password);
//            User user1 = null;
//            user1 = userService.loginByPhone(user3);
//            if (user1 != null) {
//                request.getSession().setAttribute("user", user1);
//                return new Result<User>(200, true, null, "登录成功");
//            }
//        } else if (username.matches(email)) {
//            User user3 = new User();
//            user.setEmail(username);
//            user.setPassword(password);
//            User user2 = null;
//            user2 = userService.loginByEmail(user3);
//            if (user2 != null) {
//                request.getSession().setAttribute("user", user2);
//                return new Result<User>(200, true, null, "登录成功");
//            }
//        }
//        return new Result<User>(201, false, null, "登录失败");
//    }

    @ApiOperation(value = "显示用户名")
    @GetMapping("display")
    public Result display(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        if (user != null) {
            return new Result(200, true, user, "根据session查询用户成功");
        }
        return new Result(201, false, null, "根据session查询用户信息失败");
    }

    @ApiOperation(value = "退出账号")
    @GetMapping(value = "exit")
    public Result<User> exit(HttpServletRequest request) {
        request.getSession().invalidate();
        return new Result<User>(200, true, null, "退出成功");
    }

    @ApiOperation(value = "判断邮箱是否可用")
    @GetMapping(value = "ifAvailableEmail/{email}")
    @ApiImplicitParam(name = "email", value = "邮箱", required = true, type = "String")
    public Result<User> ifAvailableEmail(@PathVariable("email") String email) {
        QueryWrapper<User> wrapper = new QueryWrapper<User>();
        wrapper.eq("email", email);
        if (userService.count(wrapper) <= 0) {
            return new Result<User>(200, true, null, "邮箱可用");
        }
        return new Result<User>(201, false, null, "邮箱已存在");
    }

    @ApiOperation(value = "判断手机号是否可用")
    @GetMapping(value = "ifAvailiablePhone/{telephone}")
    @ApiImplicitParam(name = "telephone", value = "手机号", required = true, type = "String")
    public Result<User> ifAvaliablePhone(@PathVariable("telephone") String telephone) {
        QueryWrapper queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("telephone", telephone);
        if (userService.count(queryWrapper) <= 0) {
            return new Result<User>(200, true, null, "手机号可用");
        }
        return new Result<User>(201, false, null, "手机号已存在");
    }

    @PostMapping(value = "getUserAll")
    @ApiOperation(value = "查询所有用户信息")
    public Result<User> getUserAll() {
        List<User> users = userService.list(null);
        if (users.size() > 0) {
            return new Result<User>(200, true, users, "查询所有用户成功");
        }
        return new Result<User>(201, false, null, "查询所有用户失败");
    }

}